//
//  TabBarView.swift
//  BasicTools
//
//  Created by ikang on 2025/8/20.
//

import SwiftUI

#if os(iOS)
public struct TabBarView<T: View>: View {
    
    @Bindable public var barModel: TabBarModel
    @ViewBuilder public var content: () -> T
    
//    @State private var position: ScrollPosition = ScrollPosition(x: 0)
    
    public init(barModel: TabBarModel, @ViewBuilder content: @escaping () -> T) {
        self.barModel = barModel
        self.content = content
    }
    
    public var body: some View {
        ZStack(alignment: .bottom) {
            ScrollViewReader { proxy in
                ScrollView(.horizontal) { 
                    HStack { 
                        content()
                    }
                }
                .scrollDisabled(true)
                .onChange(of: barModel.gridModel.selectIndex) { oldValue, newValue in
                    proxy.scrollTo(newValue, anchor: .center)
                }
            }

            TabGridView(gridModel: barModel.gridModel)
                .padding(barModel.padding)
                .background(barModel.backgroundColor)
        }
        .ignoresSafeArea(.all)
    }
}

#endif

//#Preview {
//    TabBarView(selectIndex: )
//}
